import sys, uuid, os, time, datetime, re
from math import *
os.environ['MPLCONFIGDIR']='/tmp/'
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pylab 
from matplotlib.ticker import ScalarFormatter
from matplotlib import rc
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
from matplotlib.cbook import iterable
import numpy as np

WEB2PY_ROOT = os.getenv('WEB2PY_ROOT')

universal_converter = os.path.join(request.folder,'private/development/Converters/universal_converter.py')

make_plaquette = os.path.join(request.folder,'private/development/Libraries/make_plaquettes.exe')


def tmpname():
    import uuid
    return 'tmp.%s' % uuid.uuid4()

def mdp_postfix(text):
    i = text.rfind('.')
    if i<0: return text+'.mdp'
    return text[:i]+'.mdp'

def rawfile_to_mdpfile(rawfile,local):
    tmpfile = tmpname()
    if local:
        command='python %s %s > %s ' % (universal_converter, rawfile, tmpfile)
    else:
        command='python %s %s > %s ' % (universal_converter, locate_file(rawfile), tmpfile)
    logging.info(command)
    os.system(command)
    if local:
        rawfilename=rawfile.split('/')[-1]
        mdpfile=mdp_postfix(rawfilename)
        command = 'mv %s.mdp %s' % (rawfilename,locate_file(mdpfile))
    else:
        mdpfile=mdp_postfix(rawfile)
        command = 'mv %s.mdp %s' % (rawfile,locate_file(mdpfile))
    logging.info(command)
    os.system(command)
    os.unlink(tmpfile)
    logging.info('done!')
    return mdpfile
              
def mdpfile_to_plaquette(mdpfile,local): # converted file
    tmpfile = tmpname()
    if local:
        command='%s %s > %s' % (make_plaquette, mdpfile, tmpfile)
    else:
        command='%s %s > %s' % (make_plaquette, locate_file(mdpfile), tmpfile)
    logging.info(command)
    os.system(command)
    logging.info('searching output for pattern')    
    match = re.compile('plaquette\: (?P<p>\-?\d.\d+)').search(open(tmpfile,'rb').read())
    p=match.group('p')
    logging.info('done!')    
    os.unlink(tmpfile)
    if match: return float(match.group('p'))
    
def plot_plaquette(stream_id):
    logging.info('plot_plaquette')
    stream = db.stream[stream_id]
    configs = db(db.configfile.stream==stream_id).select()
    malist=[]
    plaquette=[]
    maplaquette=[]
    x=[]
    for config in configs:
        if config.plaquette!= None and config.plaquette!='<NULL>':
            malist.append((config.filename,config.plaquette))
    malist=sorted(malist)
    if malist!=[]:
        maplaquette.append(malist[0][1])
        plaquette.append(malist[0][1])
    else:
        maplaquette.append(0)
        plaquette.append(0)
    for i in range(1,len(malist)):
        maplaquette.append((maplaquette[i-1]*i+malist[i][1])/(i+1))
        plaquette.append(malist[i][1])
    for i in range(len(maplaquette)):
        x.append(i)
    fig=plt.figure()
    ax = fig.add_subplot(111)#.yaxis
    #ax=fig.yaxis
    plt.plot(x,maplaquette)
    plt.title('Plaquette Moving Average')
    #ax.yaxis.major.formatter.set_scientific(False)
    #ax.tick.ScalarFormatter(useOffset=False)
    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    plt.savefig('/tmp/maplotfig',format='png')
    maplotfile = '/tmp/maplotfig'
    stream.update_record(maplot=db.stream.maplot.store(open(maplotfile,'rb'),maplotfile+".png"))
    db.commit()
    os.unlink(maplotfile)
    fig2=plt.figure()
    ax = fig2.add_subplot(111)
    plt.plot(x,plaquette)
    plt.title('Plaquette')
    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    plt.savefig('/tmp/plplotfig',format='png')
    plplotfile = '/tmp/plplotfig'
    stream.update_record(plplot=db.stream.plplot.store(open(plplotfile,'rb'),plplotfile+".png"))
    os.unlink(plplotfile)
    db.commit()
    logging.info('done!')

def plaquette(id=None):
    if id is None:
        raise Exception('id is required')
    configfile = db(db.configfile.id==id).select().first()
    if configfile is None:
        raise Exception('no configfile #%s.' % id)
    if configfile.status==COMPLETED:
        return
    try:
        configfile.update_record(status = PROCESSING)
        db.commit()
        if not configfile.file.endswith('.mdp'):
            logging.info('converting %s' % configfile.file)
            mdpfile=rawfile_to_mdpfile(configfile.file,configfile.local)
            configfile.update_record(converted_file=mdpfile)
        else:
            mdpfile = configfile.file
            configfile.update_record(converted_file=mdpfile)
            configfile.update_record(localmdp=True)
        db.commit()
        logging.info('computing plaquette %s' % configfile.converted_file)
        configfile.update_record(plaquette=mdpfile_to_plaquette(mdpfile,configfile.localmdp))
        db.commit() 
        plot_plaquette(configfile.stream)    
        configfile.update_record(status=COMPLETED)
        db.commit()
    except Exception, e:
        logging.error('processing configfile #%s: %s' % (id,e))
        configfile.update_record(status=FAILED)
        db.commit()

id = int(sys.argv[-1])
plaquette(id)

